package com.fb;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import com.dev.IconViewClose;
import com.dev.MainActivity;
import com.dev.ShowSoftInput;
import com.dev.TextButton;
import com.fb.FileBrowserPage;
import com.log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.PrintStream;

class QueryCreate extends LinearLayout implements OnClickListener,OnEditorActionListener{
	WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
	TextView title;
	LinearLayout titlebar;
	LinearLayout bottombar;
	EditText editor;
	IconViewClose close;
	int createType=0;
	ShowSoftInput ssi;
	TextButton tbCreate;
	LayoutParams tblp;

	FileBrowserPage fbp;
	MainActivity ma;
	QueryCreate(MainActivity ma){
		super(ma);
		this.ma=ma;
		setOrientation(VERTICAL);
		setWillNotDraw(false);
		title=new TextView(ma);
		title.setTextColor(0xff000000);
		title.setTextSize(TypedValue.COMPLEX_UNIT_DIP,14);
		title.setGravity(Gravity.CENTER_VERTICAL);
		title.setPadding(9,0,0,0);
		close=new IconViewClose(ma);
		close.setOnClickListener(this);
		ssi=new ShowSoftInput(ma);
		ssi.setOnClickListener(this);
		titlebar=new LinearLayout(ma){
			public void onDraw(Canvas cvs){
				super.onDraw(cvs);
				Paint p=new Paint();
				p.setColor(FileBrowserPage.borderColor);
				cvs.drawRect(0,getHeight()-FileBrowserPage.borderWidth,getWidth(),getHeight(),p);
			}
		};
		titlebar.setWillNotDraw(false);
		titlebar.setOrientation(HORIZONTAL);
		titlebar.addView(title,new LayoutParams(0,-1,1));
		titlebar.addView(ssi);
		titlebar.addView(close);
		tblp=new LayoutParams(0,-1,1);
		tbCreate=new TextButton(ma,"创建");
		tbCreate.borderColor=FileBrowserPage.borderColor;
		tbCreate.borderWidth=FileBrowserPage.borderWidth;
		tbCreate.setOnClickListener(this);
		bottombar=new LinearLayout(ma){
			@Override
			public void onDraw(Canvas c){
				super.onDraw(c);
				Paint p=new Paint();
				p.setColor(FileBrowserPage.borderColor);
				c.drawRect(0,0,getWidth(),FileBrowserPage.borderWidth,p);
			}
		};
		bottombar.setWillNotDraw(false);
		bottombar.setOrientation(HORIZONTAL);
		bottombar.addView(tbCreate,tblp);
		editor=new EditText(ma){
			public void onDraw(Canvas c){
				super.onDraw(c);
				Paint p=new Paint();
				p.setColor(0xffff0000);
				int hor=getHeight()/2+(int)(getTextSize()*0.7);
				c.drawRect(getScrollX()+getPaddingLeft(),hor,getWidth()+getScrollX()-getPaddingRight(),hor-2,p);
			}
		};
		editor.setTextSize(TypedValue.COMPLEX_UNIT_DIP,13);
		editor.setPadding(9,0,9,0);
		editor.setTypeface(Typeface.MONOSPACE);
		editor.setHorizontallyScrolling(true);
		editor.setSingleLine();
		editor.setBackgroundResource(0);
		editor.setTextColor(0xff000000);
		editor.setGravity(Gravity.CENTER_VERTICAL);
		editor.setOnEditorActionListener(this);
		addView(titlebar,-1,ma.dialogTitleBarHeight);
		addView(editor,-1,(int)(30*ma.dp));
		addView(bottombar,-1,(int)(30*ma.dp));
	}

	@Override
	public void onClick(View v){
		if(v==close){//onClick
			dismiss();
		}else if(v==ssi){//onClick
			if(editor.requestFocus()){
				if(editor.requestFocus()){
					ma.imm.showSoftInput(editor,0);
					editor.selectAll();
				}
			}
		}else if(v==tbCreate){//onClick
			String usrinput=editor.getText().toString();
			if(usrinput.contains("/")){
				Toast.makeText(ma,"不能包含字符'/'",0).show();
				return;
			}
			File f=new File(fbp.currentDirectory,editor.getEditableText().toString());
			if(createType==0){
				if(f.exists()){
					Toast.makeText(ma,"文件已存在",0).show();
				}else
					try{
						if(f.createNewFile()){
							Toast.makeText(ma,"文件已创建",0).show();
							dismiss();
							fbp.setWillShow(fbp.currentDirectory.listFiles());

						}else{
							Toast.makeText(ma,"创建文件失败",0).show();
						}
					}catch(Exception e){
						log.alert("创建文件时出现异常",e.toString());
					}
			}else if(createType==1){
				if(f.exists()){
					Toast.makeText(ma,"文件夹已存在",0).show();
				}else 
					try{
						if(f.mkdir()){
							Toast.makeText(ma,"文件夹已创建",0).show();
							dismiss();
							fbp.setWillShow(fbp.currentDirectory.listFiles());
						}else{
							Toast.makeText(ma,"创建文件夹失败",0).show();
						}
					}catch(Exception e){
						log.alert("创建文件夹时出现问题",e.toString());
					}
			}else if(createType==2){
				if(f.exists()){
					Toast.makeText(ma,"文件已存在",0).show();
				}else{
					try{
						if(f.createNewFile()){
							FileWriter fw=new FileWriter(f);
							fw.write("<svg xmlns=\"http://www.w3.org/2000/svg\"\n\tversion=\"1.1\"\n\twidth=\"100\"\n\theight=\"100\">\n");
							fw.write("</svg>");
							fw.close();
							dismiss();
							Toast.makeText(ma,"已创建",0).show();
							fbp.setWillShow(fbp.currentDirectory.listFiles());
						}else{
							Toast.makeText(ma,"can not create new file",0).show();
						}
					}catch(Exception e){
						log.alert(e.toString());
					}
				}
			}else if(createType==3){
				if(f.exists()){
					Toast.makeText(ma,"文件已存在",0).show();
				}else{
					try{
						if(f.createNewFile()){
							FileWriter fw=new FileWriter(f);
							fw.write("<!doctype html>\n");
							fw.write("<html>\n");
							fw.write("<head>\n");
							fw.write("\t<title>MyTitle</title>\n");
							fw.write("</head>\n");
							fw.write("<body>\n");
							fw.write("</body>\n");
							fw.write("</html>\n");
							fw.close();
							dismiss();
							Toast.makeText(ma,"已创建",0).show();
						}else{
							Toast.makeText(ma,"创建文件失败",0).show();
						}
					}catch(Exception e){
						log.alert(e.toString());
					}
				}
			}else if(createType==4){
				if(f.exists()){
					Toast.makeText(ma,"文件已存在",0).show();
				}else{
					try{
						if(f.createNewFile()){
							FileOutputStream fos=new FileOutputStream(f,false);
							PrintStream ps=new PrintStream(fos);
							ps.println("<?xml version='1.0' encoding='utf-8'?>");
							ps.println("<vector xmlns:android=\"http://schemas.android.com/apk/res/android\"");
							ps.println("\tandroid:width=\"24dp\"");
							ps.println("\tandroid:height=\"24dp\"");
							ps.println("\tandroid:viewportWidth=\"24\"");
							ps.println("\tandroid:viewportHeight=\"24\"");
							ps.println("\tandroid:name=\"My vector\"");
							ps.println("\tandroid:tint=\"#ffffff\">");
							ps.println("</vector>");
							ps.close();
							fos.close();
							dismiss();
						}else{
							Toast.makeText(ma,"创建文件失败",0).show();
						}
					}catch(Exception e){
						log.alert(e.toString());
					}
				}
			}

		}
	}
	@Override
	public boolean onEditorAction(TextView v,int actionId,KeyEvent event){
		return false;
	}
	@Override
	protected void onDraw(Canvas canvas){
		Paint p=new Paint();
		p.setColor(fbp.borderColor);
		p.setStyle(Paint.Style.FILL);
		canvas.drawColor(0xffffffff);
		canvas.drawRect(0,0,fbp.borderWidth,getHeight(),p);
		canvas.drawRect(0,0,getWidth(),fbp.borderWidth,p);
		canvas.drawRect(getWidth()-fbp.borderWidth,0,getWidth(),getHeight(),p);
		canvas.drawRect(0,getHeight()-fbp.borderWidth,getWidth(),getHeight(),p);
		super.onDraw(canvas);
	}
	void show(FileBrowserPage fbp){
		this.fbp=fbp;
		wlp.width=fbp.getWidth()*3/4;
		wlp.height=-2;
		ma.wm.addView(this,wlp);

		if(createType==0){
			title.setText("新建文件");
			for(int i=1;i<100;i++){
				File f=new File(fbp.currentDirectory,"新建文件"+i);
				if(!f.exists()){
					editor.setText(f.getName());
					editor.selectAll();
					break;
				}
			}

		}else if(createType==1){
			title.setText("新建文件夹");
			for(int i=1;i<100;i++){
				File f=new File(fbp.currentDirectory,"新建文件夹"+i);
				if(!f.exists()){
					editor.setText(f.getName());
					editor.selectAll();
					break;
				}
			}
		}else if(createType==2){//new svg file
			title.setText("新建svg");
			final String defaultname="test.svg";
			editor.setText(defaultname);
			editor.setSelection(0,4);
		}else if(createType==3){
			title.setText("新建html");
			final String defaultname="index.html";
			editor.setText(defaultname);
			editor.setSelection(0,5);
		}else if(createType==4){
			title.setText("新建xml");
			final String defaultname="test.xml";
			editor.setText(defaultname);
			editor.setSelection(0,defaultname.lastIndexOf("."));
		}else if(createType==5){
			title.setText("新建当前目录的软链接");
			String name=fbp.currentDirectory.getName();
			editor.setText(name);
		}
	}
	void dismiss(){
		ma.wm.removeView(this);
	}
}
